package tt.com.warehousemanager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import tt.com.common.model.YxCommonWarehouse;
import tt.com.common.model.YxCommonWarehouseGoods;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.mapper.WarehouseMapper;
import tt.com.warehousemanager.service.WarehouseService;

import java.util.Date;

/**
 * @author Czycka
 * @date 2022/8/24 3:04
 * @description
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, YxCommonWarehouse> implements WarehouseService {
    @Override
    public Integer insertWareHouse(YxCommonWarehouse warehouse, String createUserId) {
        // 设置创建时间
        warehouse.setCreateTime(new Date());
        // 设置创建操作人id
        warehouse.setCreateUserId(createUserId);
        return baseMapper.insert(warehouse);
    }

    @Override
    public Integer deleteWareHouse(Long id, String deleteUserId) {
        YxCommonWarehouse warehouse = baseMapper.selectById(id);
        if (warehouse == null) {
            // 若仓库不存在则返回 -1
            return -1;
        }
        if (warehouse.getDataStatus() == 1) {
            // 仓库已被删除则返回 -2
            return -2;
        }
        // 设置删除时间
        warehouse.setDeleteTime(new Date());
        // 设置删除操作人id
        warehouse.setDeleteUserId(deleteUserId);
        // 设置删除状态
        baseMapper.updateById(warehouse);
        return baseMapper.deleteById(warehouse.getId());
    }

    @Override
    public Integer updateWareHouse(YxCommonWarehouse warehouse, String updateUserId) {
        // 设置修改时间
        warehouse.setLastUpdateTime(new Date());
        // 设置修改操作人id
        warehouse.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(warehouse);
    }

    @Override
    public PageUtil showWareHouseByPage(PageUtil pageUtil) {
        Page<YxCommonWarehouse> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<YxCommonWarehouse> warehousePage = baseMapper.selectPage(page, null);
        pageUtil.setTotalSize((int) warehousePage.getTotal());
        pageUtil.setData(warehousePage);
        return pageUtil;
    }
}
